/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.FxCmsInfo;
import com.ykht.proj.coupon.entity.FxComment;
import com.ykht.proj.coupon.entity.FxCommentUp;
import com.ykht.proj.coupon.mapper.FxCommentMapper;
import com.ykht.proj.coupon.mapper.FxCommentUpMapper;
import com.ykht.proj.coupon.service.FxCommentUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 评论点赞表
 *
 * @author proj code generator
 * @date 2021-07-07 16:20:00
 */
@Service
@SuppressWarnings("all")
public class FxCommentUpServiceImpl extends ServiceImpl<FxCommentUpMapper, FxCommentUp> implements FxCommentUpService {


	@Autowired
	private FxCommentMapper fxCommentMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void up(FxCommentUp fxCommentUp) {
		FxCommentUp up = new FxCommentUp().selectOne(Wrappers.lambdaQuery(FxCommentUp.class)
				.eq(FxCommentUp::getCommentId, fxCommentUp.getCommentId()).eq(FxCommentUp::getPerId, SecurityUtils.getPersonId()));
		FxCmsInfo cmsInfo = new FxCmsInfo().selectById(fxCommentUp.getCmsId());
		if (up != null){
			this.removeById(up.getId());
			Integer num = fxCommentMapper.updateUpCount(fxCommentUp.getCommentId(),-1);
		}else {
			FxCommentUp commentUp = new FxCommentUp();
			commentUp.setCompanyId(cmsInfo.getCompanyId());
			commentUp.setBrandId(cmsInfo.getBrandId());
			commentUp.setCmsId(cmsInfo.getId());
			commentUp.setCommentId(fxCommentUp.getCommentId());
			commentUp.setPerId(SecurityUtils.getPersonId());
			commentUp.setPerPic(SecurityUtils.getPerson().getHeadPortrait());
			commentUp.setCreateUser(SecurityUtils.getPersonId());
			commentUp.setCreateTime(LocalDateTime.now());
			commentUp.insert();
			Integer num = fxCommentMapper.updateUpCount(fxCommentUp.getCommentId(),1);
		}
	}
}
